<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="85px"
    >
      <el-form-item label="公司名称:" prop="compName">
        <el-input
          v-model="queryParams.compName"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拜访时间:">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
            <el-button type="danger" plain 
            icon="el-icon-delete" size="mini" :disabled="multiple" 
            @click="handleDelete">删除
            </el-button>
          </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="kqMaintainInfoList"
      height="650"
      stripe
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="index" width="38" align="center" /> -->
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column
        label="拜访时间"
        align="center"
        prop="create_datetime"
        show-overflow-tooltip
      />
      <el-table-column label="公司名称" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.compName"
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="CopyFileUrl(scope.row.compName)"
          />
          <el-link
            target="_blank"
            :href="
              'https://www.tianyancha.com/search?key=' + scope.row.compName
            "
          >
            {{ scope.row.compName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="拜访目的"
        align="center"
        prop="purpose"
        show-overflow-tooltip
      />

      <el-table-column
        label="拜访详情"
        align="center"
        prop="extendDesc"
        show-overflow-tooltip
      />
      <el-table-column
        label="拜访效果"
        align="center"
        prop="score"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-rate v-model="scope.row.score" class-name="rateClass"></el-rate>
        </template>

      </el-table-column>

      <el-table-column label="沟通方式" align="center" show-overflow-tooltip>

        <template slot-scope="scope">
          <el-tooltip v-if="scope.row.maintainType=='电话'" content="电话沟通" placement="top">
            <el-button
              type="primary"
              icon="el-icon-phone"
              circle
            ></el-button>
          </el-tooltip>
          <el-tooltip v-if="scope.row.maintainType=='拜访'" content="实地拜访" placement="top">
            <el-button
              type="primary"
              icon="el-icon-s-home"
              circle
            ></el-button>
          </el-tooltip>
          <el-tooltip v-if="scope.row.maintainType=='微信'" content="微信沟通" placement="top">
            <el-button
              type="primary"
              icon="el-icon-chat-dot-round"
              circle
            ></el-button>
          </el-tooltip>
          <el-tooltip v-if="scope.row.maintainType=='邮件'" content="邮件沟通" placement="top">
            <el-button
              type="primary"
              icon="el-icon-message"
              circle
            ></el-button>
          </el-tooltip>

        </template>

      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            :loading="modifyLoading"
            @click="handleModify(scope.row)"
          />
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click="handleDelete(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- <el-dialog :title="title" :visible.sync="open" append-to-body width="80%"> -->
      <el-drawer
      :title="title"
      :visible.sync="open"
      size="65%"
      :with-header="true"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-form-item
            label-width="100px"
            label="拜访方式:"
            prop="maintainType"
          >
            <el-radio-group v-model="form.maintainType">
              <el-radio label="电话" value="电话"></el-radio>
              <el-radio label="拜访" value="拜访"></el-radio>
              <el-radio label="微信" value="微信"></el-radio>
              <el-radio label="邮件" value="邮件"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="联系人:" prop="name">
              <el-input v-model="form.name" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item
              label-width="100px"
              label="联系人职位:"
              prop="position"
            >
              <el-input
                v-model="form.position"
                placeholder="请输入联系人职位"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="联系人电话:" prop="phone">
              <el-input v-model="form.phone" placeholder="联系人电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="公司名称:" prop="compName">
              <el-select
                v-model="form.compName"
                class="compName"
                filterable
                remote
                clearable
                placeholder="请输入公司名称"
                style="width: 100%"
                :remote-method="remoteMethod"
                @change="handleChangeCompNameFunc"
              >
                <el-option
                  v-for="item in compNameOptions"
                  :id="item.id"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item
              label-width="100px"
              label="拜访目的:"
              prop="purpose"
            >
              <el-input
                v-model="form.purpose"
                placeholder="输入拜访目的"
              />
            </el-form-item>
          </el-col>

        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item
              label-width="100px"
              label="拜访详情:"
              prop="extendDesc"
            >
              <el-input
                v-model="form.extendDesc"
                minlength="500"
                type="textarea"
                placeholder="拜访详情"
                :rows="8"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="评 分:" prop="score" label-width="100px">
            <el-rate v-model="form.score" show-text></el-rate>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    <!-- </el-dialog> -->
  </el-drawer>
  </div>
</template>

<script>
import {
  getKqMaintainInfosList,
  exportKqMaintainInfos,
  addKqMaintainInfo,
  updateKqMaintainInfo,
  getKqMaintainInfo,
  delKqMaintainInfo
} from "@/api/work/kqMaintainInfo";
import { listKqAllInfos } from "@/api/work/work";

export default {
  name: "Work",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      kqMaintainInfoList: [],
      // classifyList: [],
      // 日期范围
      dateRange: [],
      modifyLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        compName: undefined
        // collaborator: undefined,
        // city: undefined,
        // district: undefined
      },
      kqAllInfoList: [],
      compNameOptions: [],
      compNameParams: {
        pageNum: 1,
        pageSize: 10,
        compName: undefined,
        isDelete: false,
        isMyCustomer: true
      },
      open: false,
      title: "",
      // 表单参数
      form: {
        maintainType: "电话"
      },
      rules: {
        compName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        purpose: [
          { required: true, message: "拜访目的不能为空", trigger: "blur" }
        ],
        extendDesc: [
          { required: true, message: "拜访详情不能为空", trigger: "blur" }
        ],
        score: [
          { required: true, message: "评分不能为空", trigger: "blur" }
        ],
        maintainType: [
          { required: true, message: "拜访方式不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      getKqMaintainInfosList(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then((res) => {
        this.kqMaintainInfoList = res.data.results;
        this.total = res.data.count;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据";
    },
    handleModify(row) {
      this.modifyLoading = true;
      this.reset();
      const id = row.id;
      getKqMaintainInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据";
        this.modifyLoading = false;
        // if (this.form.classify) {
        //   this.classifyList = this.form.classify.split(",");
        // }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const id = row.id;
      const id = row.id || this.ids;
      this.$confirm(
        '是否确认删除岗位编号为"' + row.compName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delKqMaintainInfo(id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    submitForm: function() {
      // this.form.classify = this.classifyList.length
      //   ? this.classifyList.toString()
      //   : "";
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateKqMaintainInfo(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKqMaintainInfo(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        kqId: undefined,
        compName: undefined,
        name: undefined,
        position: undefined,
        phone: undefined,
        extendDesc: undefined,
        create_datetime: undefined,
        maintainType: "电话"
      };
      // this.classifyList = [];
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有岗位数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportKqMaintainInfos(queryParams);
        })
        .then((response) => {
          this.download(response.data.file_url, response.data.name);
        });
    },
    handleChangeCompNameFunc(val) {
      this.form.kqId = this.compNameOptions.find(one => one.value == val).id;
    },
    CopyFileUrl(values) {
      const input = document.createElement("input");
      document.body.appendChild(input);
      input.setAttribute("value", values);
      input.select();
      if (document.execCommand("copy")) {
        document.execCommand("copy");
        this.msgSuccess("复制成功");
      }
      document.body.removeChild(input);
    },
    remoteMethod(query) {
      if (query !== "") {
        this.loading = true;
        this.compNameParams.compName = query;
        listKqAllInfos(this.compNameParams).then((res) => {
          this.compNameOptions = [];
          this.kqAllInfoList = res.data.results;
          this.kqAllInfoList.forEach((one) => {
            this.compNameOptions.push({
              id: one.id,
              value: one.compName,
              label: one.compName
            });
          });
        });
      } else {
        this.compNameOptions = [];
      }
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">

.el-rate{
  line-height:1;
}
.rateClass{
   line-height:1.5;
}
</style>
